Star (-) Watch (-)

Blog

Garbage Collector Automatic garbage collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. All Garbage Collector involves in making sure the heap does not run out of space. The object is not reachable when no live thread can access it. Objects are created on heap in Java irrespective of their scope. Class variables or static members are created in method area of Java memory space and both heap and method area is shared between different thread. You can make an object garbage collected by : Explicitly assigning a reference to null Reassigning the reference to another object will cause older object to be eligible for Garbage Collector. After the method return the local variables in that method will be automatically eligible for Garbage Collector Even if objects have valid references, but still there is no way to reach them, they will be Garbage Collector'ed. Garbage collection relieves java programmer from memory management. Java Virtual Machine throws OutOfMemoryError or java.lang.OutOfMemoryError when there is no memory space for creating new object in heap. Garbage Collection in Java is carried by a daemon thread called Garbage Collector.You can invoke Garbage Collector manually using the methods Runtime.getRuntime().gc() or System.gc(), but there is no guarantee that Garbage Collector will happen. The code written in finalize method are executed before the object is Garbage Collectoed and gives an opportunity to perform any sort of cleanup required.. The finalize() method is defined in Object. The Serial GC: With the serial collector, both minor and major garbage collections are done serially. it uses a mark-compact collection method.This method moves older memory to the beginning of the heap so that new memory allocations are made into a single continuous chunk of memory at the end of the heap. This compacting of memory makes it faster to allocate new chunks of memory to the heap. The Parallel GC: The parallel garbage collector uses multiple threads to perform the young generation garbage collection.